package com.yuw.androidsoundplayer;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import com.yuw.androidsoundplayer.util.PermissionUtil;

public class MainActivity extends AppCompatActivity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_music_player).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
       if (v.getId() == R.id.btn_music_player) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                Toast.makeText(this, "歌词滚动的暂停与恢复功能需要Android4.4或以上版本", Toast.LENGTH_SHORT).show();
                return;
            }
            if (PermissionUtil.checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, R.id.btn_music_player % 4096)) {
                PermissionUtil.goActivity(this, MusicPlayerActivity.class);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == R.id.btn_music_player % 4096) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                PermissionUtil.goActivity(this, MusicPlayerActivity.class);
            } else {
                Toast.makeText(this, "需要允许SD卡权限才能播放音乐噢", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
